package it.uniroma1.di.machinelearning;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

public class LinuxCommands {

	private static Logger log = Logger.getLogger(LinuxCommands.class);

	public static void main(String[] args) {
		try {
			Runtime r = Runtime.getRuntime();
			Process p = r.exec("ls");
			p.waitFor();
			BufferedReader b = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			String line = "";

			while ((line = b.readLine()) != null) {
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("finished.");
	}

	public static String shuffle(String inputFilePath, String outputFilePath) {

		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("shuf ");
		stringBuilder.append(inputFilePath);
		stringBuilder.append(" -o ");
		stringBuilder.append(outputFilePath);

		return executeLinuxCommand(stringBuilder.toString());
	}

	public static String append(String inputFilePath, String outputFilePath) {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("cat ");
		stringBuilder.append(inputFilePath);
		stringBuilder.append(" >> ");
		stringBuilder.append(outputFilePath);

		return executeLinuxCommand(stringBuilder.toString());
	}

	public static String split(int numberOfLines, String fileToSplit,
			String prefix) {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("split -l ");
		stringBuilder.append(numberOfLines);
		stringBuilder.append(" ");
		stringBuilder.append(fileToSplit);
		stringBuilder.append(" ");
		stringBuilder.append(prefix);

		return executeLinuxCommand(stringBuilder.toString());
	}

	public static String executeLinuxCommand(String command) {

		log.debug("Command to execute: " + command);

		try {
			/*
			 * Runtime r = Runtime.getRuntime(); Process p = r.exec(command);
			 * p.waitFor(); byte[] error = new byte[1024];
			 * p.getErrorStream().read(error); log.debug("Error stream: " +
			 * error); BufferedReader b = new BufferedReader(new
			 * InputStreamReader( p.getInputStream())); String line = "";
			 * 
			 * StringBuilder builder = new StringBuilder();
			 * 
			 * while ((line = b.readLine()) != null) { builder.append(line);
			 * builder.append('\n'); }
			 * 
			 * return builder.toString();
			 */

			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec(command);
			InputStream stderr = proc.getErrorStream();
			InputStreamReader isr = new InputStreamReader(stderr);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			System.out.println("<ERROR>");
			while ((line = br.readLine()) != null)
				System.out.println(line);
			System.out.println("</ERROR>");
			int exitVal = proc.waitFor();
			System.out.println("Process exitValue: " + exitVal);

		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}
}
